const cwd = process.cwd()
const path = require('path')
const webpack = require('webpack')
const CssExtractPlugin = require('mini-css-extract-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin')
const OptimizeCssPlugin = require('optimize-css-assets-webpack-plugin')
const { CleanWebpackPlugin: CleanPlugin } = require('clean-webpack-plugin')

module.exports = {
  site: {
    title: 'blog',
    author: '',
    subtitle: 'my new blog',
    description: '',
    keywords: 'dait, blog, vue, gitee',
    language: 'zh-CN'
  },
  theme: 'daiter',
  configureWebpack: {
    entry: '',
    output: path.join(cwd, './dist'),
    alias: {
      '@': path.join(cwd, './'),
      '@theme': ''
    },
    rules: [],
    plugins: [],
    defineData: {},
    dev: {
      devtool: 'cheap-module-eval-source-map',
      devServer: {
        open: false,
        hot: true,
        port: 6800,
        clientLogLevel: 'warning'
      },
      rules: [
        {
          test: /\.css$/,
          use: [
            'vue-style-loader',
            {
              loader: 'css-loader',
              options: {
                esModule: false
              }
            },
            {
              loader: 'postcss-loader',
              options: {
                postcssOptions: {
                  config: path.join(__dirname, './postcss.config.js')
                }
              }
            }
          ]
        },
        {
          test: /\.less$/,
          use: [
            'vue-style-loader',
            {
              loader: 'css-loader',
              options: {
                esModule: false
              }
            },
            {
              loader: 'postcss-loader',
              options: {
                postcssOptions: {
                  config: path.join(__dirname, './postcss.config.js')
                }
              }
            },
            'less-loader'
          ]
        },
        {
          test: /\.vue$/,
          enforce: 'pre',
          exclude: [
            path.join(cwd, './node_modules'),
            path.resolve('./node_modules')
          ],
          loader: 'eslint-loader'
        },
        {
          test: /\.js$/,
          enforce: 'pre',
          exclude: [
            path.join(cwd, './node_modules'),
            path.resolve('./node_modules')
          ],
          loader: 'eslint-loader'
        }
      ],
      plugins: [
        new webpack.HotModuleReplacementPlugin()
      ]
    },
    prod: {
      devtool: 'none',
      optimization: {
        minimize: true,
        minimizer: [
          new OptimizeCssPlugin(),
          new TerserWebpackPlugin()
        ],
        splitChunks: {
          chunks: 'all',
          cacheGroups: {
            libs: {
              name: 'depend/chunk-libs',
              test: /[\\/]node_modules[\\/]/,
              priority: 10,
              chunks: 'initial' // 只打包初始时依赖的第三方
            },
            elementUI: {
              name: 'depend/chunk-elementUI', // 单独将 elementUI 拆包
              priority: 20, // 权重
              test: /[\\/]node_modules[\\/]element-ui[\\/]/
            },
            vueLibs: {
              name: 'depend/chunk-vue', // 单独将 vue 开头的依赖 拆包
              priority: 15, // 权重
              test: /[\\/]node_modules[\\/]vue/
            }
          }
        },
        runtimeChunk: 'single'
      },
      rules: [
        {
          test: /\.css$/,
          use: [
            CssExtractPlugin.loader,
            'css-loader',
            {
              loader: 'postcss-loader',
              options: {
                postcssOptions: {
                  config: path.join(__dirname, './postcss.config.js')
                }
              }
            }
          ]
        },
        {
          test: /\.less$/,
          use: [
            CssExtractPlugin.loader,
            'css-loader',
            {
              loader: 'postcss-loader',
              options: {
                postcssOptions: {
                  config: path.join(__dirname, './postcss.config.js')
                }
              }
            },
            'less-loader'
          ]
        }
      ],
      plugins: [
        new CleanPlugin(),
        new CssExtractPlugin({
          filename: 'css/[name]-[contenthash:8].css'
        })
      ]
    }
  },
  configurePostcss: {
    plugins: [
      require('autoprefixer'),
    ]
  }
}